C#: Using as bool? instead of object something = ViewState["hi"]

Posted by Programmin Tool on Stack Overflow See other posts from Stack Overflow or by Programmin Tool
Published on 2010-04-20T13:42:58Z Indexed on 2010/04/20 13:53 UTC
Read the original article Hit count: 120

Filed under:
|

So I'm going through old code (2.0) and I came across this:

object isReviewingValue = ViewState["IsReviewing"];

if (isReviewingValue is bool)
{
  return (bool)isReviewingValue;
}

My first thought was to us the "as" keyword to avoid the unneeded

(bool)isReviewingValue;

But "as" only works with non value types. No problem, I just went ahead and did this:

bool? isReviewingValue= ViewState["IsReviewing"] as bool?;
if (isReviewingValue.HasValue)
{
  return isReviewingValue.Value;
}

Question is: Besides looking a bit more readable, is this in fact better?

© Stack Overflow or respective owner

Related posts about c#

Related posts about as-keyword